/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-30
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.*;
import com.rzico.basics.mapper.ProductMapper;
import com.rzico.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.FreightMapper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 运费模板业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class FreightService extends BaseServiceImpl<Freight, String> {

    @Autowired
    private FreightMapper freightMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private FreightAreaService freightAreaService;

    @Override
    public BaseMapper<Freight, String> getMapper() {
        return freightMapper;
    }


    @Transactional(rollbackFor = Exception.class)
    public int save(Freight freight) {

        int affectCount = super.insertUseGeneratedKeys(freight);

        for (FreightArea area:freight.getItemList()) {
            area.setFreightId(freight.getId());
            area.setEnterpriseId(freight.getEnterpriseId());
            area.setPackagingFee(freight.getPackagingFee());
            freightAreaService.insertUseGeneratedKeys(area);
        }

        return affectCount;

    }

    @Transactional(rollbackFor = Exception.class)
    public int update(Freight freight) {

        int affectCount = super.updateByPrimaryKeySelective(freight);

        FreightArea area = new FreightArea();

        area.setFreightId(freight.getId());
        freightAreaService.delete(area);

        for (FreightArea freightArea:freight.getItemList()) {
            freightArea.setFreightId(freight.getId());
            freightArea.setEnterpriseId(freight.getEnterpriseId());
            freightArea.setPackagingFee(freight.getPackagingFee());
            freightAreaService.insertUseGeneratedKeys(freightArea);
        }

        return affectCount;

    }

    public BigDecimal calculate(Freight freight, BigDecimal quantity, BigDecimal distance, Integer areaId) {

        List<FreightArea> areaList = freight.getAreaItemList();

        BigDecimal ctd = freight.getCtd();
        BigDecimal ctdPrice = freight.getCtdPrice();
        BigDecimal ykg = freight.getYkg();
        BigDecimal ykgPrice = freight.getYkgPrice();

        if (areaId!=null) {

            //获取城市运费
            String idflag = String.valueOf(areaId).substring(0, 2);
            for (FreightArea area : areaList) {
                String idStr = String.valueOf(area.getAreaId()).substring(0, 2);
                if (idStr.equals(idflag)) {
                    ctd = area.getCtd();
                    ctdPrice = area.getCtdPrice();
                    ykg = area.getYkg();
                    ykgPrice = area.getYkgPrice();
                }
            }

        }

        //判断运费类型（距离/计件）

        BigDecimal wt = distance.setScale(0,BigDecimal.ROUND_UP);
        if (freight.getType().equals(1)) {
            wt = quantity;
        }

        BigDecimal amount = BigDecimal.ZERO;
        if (wt.compareTo(ykg)>0 && ctd.compareTo(BigDecimal.ZERO)>0) {
            amount = ykg.multiply(ykgPrice).setScale(2,BigDecimal.ROUND_HALF_DOWN);

            BigDecimal wc = wt.subtract(ykg).divide(ctd,2,BigDecimal.ROUND_UP);
            amount = amount.add(wc.multiply(ctdPrice).setScale(2,BigDecimal.ROUND_HALF_DOWN));

        } else {
            amount = wt.multiply(ykgPrice).setScale(2,BigDecimal.ROUND_HALF_DOWN);
        }

        return amount;

    }


    public BigDecimal calculatePackagingFee(Freight freight, BigDecimal quantity,Integer areaId) {

        List<FreightArea> areaList = freight.getAreaItemList();

        BigDecimal fee = freight.getPackagingFee();

        if (areaId!=null) {

            //获取城市运费
            String idflag = String.valueOf(areaId).substring(0, 2);
            for (FreightArea area : areaList) {
                String idStr = String.valueOf(area.getAreaId()).substring(0, 2);
                if (idStr.equals(idflag)) {
                    fee = freight.getPackagingFee();
                }
            }

        }


        BigDecimal amount = fee.multiply(quantity).setScale(2,BigDecimal.ROUND_HALF_DOWN);


        return amount;

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteByIds(Object[] ids) {

        int rw = 0;
        for (Object id:ids) {
            Map<String,Object> params = new HashMap<>();
            params.put("freightId",id);
            Integer w = productMapper.selectRowCount(params);
            if (w>0) {
                throw new CustomException("模板有被使用");
            }
            rw = rw + super.deleteByPrimaryKey(id);
        }

        return rw;

    }


}
